﻿using System;
using System.Windows.Forms;
using Application = System.Windows.Forms.Application;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;

namespace Связь_с_БД
{
    class PrintDocumentOrderOnAcadem
    {
        private string[] _strings;
        public bool IsClosed; 

        public PrintDocumentOrderOnAcadem(DataGridViewRow dataGridViewRow, string[] str)
        {
            _dataGridViewRow = dataGridViewRow;
            _strings = str;
            _orderOnAcademReplace = new string[10];
        }

        private readonly string filePath = @"\PatternDocuments\academ.docx";
        private readonly string[] _orderOnAcadem = {
                                                           "{FIO}",
                                                           "{course}",
                                                           "{group}",                                                                                                                      
                                                           "{codeDirection}",
                                                           "{nameDirection}",
                                                           "{codeProfyle}",
                                                           "{nameProfyle}",
                                                           "{reason}",
                                                           "{fromdatetodate}",
                                                           "{ground}"
                                                       };
        private string[] _orderOnAcademReplace;
        private DataGridViewRow _dataGridViewRow;

        private void SetMainCertificationValues(DataGridViewRow dgvr)
        {
            _orderOnAcademReplace[0] = dgvr.Cells[27].Value.ToString();
            _orderOnAcademReplace[1] = GetNumberCourse(dgvr.Cells[22].Value.ToString());
            _orderOnAcademReplace[2] = dgvr.Cells[22].Value.ToString();
            _orderOnAcademReplace[3] = dgvr.Cells[13].Value.ToString();
            _orderOnAcademReplace[4] = dgvr.Cells[14].Value.ToString();
            _orderOnAcademReplace[5] = dgvr.Cells[16].Value.ToString();
            _orderOnAcademReplace[6] = dgvr.Cells[15].Value.ToString();
            _orderOnAcademReplace[7] = _strings[0];
            _orderOnAcademReplace[8] = _strings[1];
            _orderOnAcademReplace[9] = _strings[2];
        }

        private void ReplaceText(string text, string replaceText, Document worddocument, Word.Application wordapp)
        {
            Object start = 0;
            Object end = worddocument.Characters.Count;
            Word.Range wordrange = worddocument.Range(ref start, ref end);
            wordrange.TextRetrievalMode.IncludeHiddenText = false;
            wordrange.TextRetrievalMode.IncludeFieldCodes = false;

            int beginphrase = 0;
            beginphrase = wordrange.Text.IndexOf(text);
            start = beginphrase;
            end = beginphrase + text.Length;
            wordrange = worddocument.Range(ref start, ref end);
            
            object unit;
            object count;
            object extend;
            
            unit = Word.WdUnits.wdStory;
            extend = Word.WdMovementType.wdMove;
            wordapp.Selection.HomeKey(ref unit, ref extend);
            
            unit = Word.WdUnits.wdCharacter;
            extend = Word.WdMovementType.wdMove;
            count = beginphrase;
            wordapp.Selection.MoveRight(ref unit, ref count, ref extend);
            
            unit = Word.WdUnits.wdCharacter;
            count = text.Length;
            extend = Word.WdMovementType.wdExtend;
            wordapp.Selection.MoveRight(ref unit, ref count, ref extend);

            wordapp.Options.Overtype = false;
            wordapp.Selection.Font.Name = @"Times New Roman";
            wordapp.Selection.Font.Size = 14;
            wordapp.Selection.Font.Color = WdColor.wdColorBlack;
            wordapp.Selection.TypeText(replaceText);
        }

        private string GetNumberCourse(string str)
        {
            string result;
            int index = str.IndexOf("-");
            index++;
            result = str.Substring(index, 1);
            return result;
        }

        void WordappDocumentBeforeClose(Document doc, ref bool cancel)
        {
            IsClosed = true;
        }

        public void InsertInDocument()
        {
            var wordapp = new Word.Application();
            wordapp.DocumentBeforeClose += WordappDocumentBeforeClose;
            string FilePath = Application.StartupPath + filePath;
            try
            {
                var worddoc = wordapp.Documents.Open(FilePath);
                SetMainCertificationValues(_dataGridViewRow);
                for (int i = 0; i < _orderOnAcademReplace.Length; i++)
                    ReplaceText(_orderOnAcadem[i], _orderOnAcademReplace[i], worddoc, wordapp);

                object nameFormat =
                    WdSaveFormat.wdFormatDocumentDefault;
                worddoc.SaveAs(Application.StartupPath + "\\okok", nameFormat);
                wordapp.Visible = true;
                IsClosed = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                wordapp.Quit();
            }
        }
    }
}
